#!/bin/bash

# BUGS
# if in the selection two files have the same basename, the last will overwrite the others

# Configuration
BASE_LISTER_URL="http://127.0.0.1/~jerome/svn/phplister3/add.php?plugin=shellscript"
BASE_LISTER_PHP="public_html/svn/phplister3/"
TMP_DIR="cache/"

# FTP configuration
FTP_SERVER="127.0.0.1"
FTP_USER="jerome"
FTP_PASS="$(cat ../password)"

# Do not modify
# FTP initialisation
FTP_DIR="${BASE_LISTER_PHP}${TMP_DIR}"
FTP="open ${FTP_SERVER}\n"
FTP="${FTP}user ${FTP_USER} ${FTP_PASS}\n"
#FTP="${FTP}cd ${FTP_DIR}\nls\n"

GET=$BASE_LISTER_URL

for FILE in $*
do
	if [ -f "${FILE}" ]
	then
		BASENAME=$(basename "${FILE}")
		FTP="${FTP}put \"${FILE}\" \"${FTP_DIR}${BASENAME}\"\n"
		FTP="${FTP}chmod 0666 \"${FTP_DIR}${BASENAME}\"\n"
		GET="${GET}&data[]=${TMP_DIR}${BASENAME}:${FILE}"
	fi
done

echo "Uploading..."
echo -e $FTP | ftp -n -i
echo $(wget "$GET" -q -O - | tail -n1)

exit 0

